package jspeech.sample;

import java.awt.AWTException;
import java.awt.event.KeyEvent;
import java.io.IOException;

import jspeech.SpeechRecognizer;
import jspeech.exception.EngineNotFoundException;
import jcommand.Action;
import jcommand.event.Application;

/**
 * Sample code for {@link jspeech.SpeechRecognizer}. This sample using the
 * {@link jspeech.SpeechRecognizer} object to recognizing the commands say by
 * the users. As the whole process is run in foreground (not a background
 * process) so, all the commands in do in the main loop. <br>
 * <br>
 * <b>Commands implemented: </b><br>
 * <li>1) Stop Listening - Stop the program</li>
 * <li>2) Internet - Open IE</li>
 * <li>3) Close Application - Close the current active application (Pressing
 * ALT+F4)</li>
 * <li>4) Switch Application - Switch to next Application (Pressing ALT+TAB)</li>
 * <li>5) Desktop - Show desktop (Pressing Windows+M)</li>
 * <br>
 * <br>
 * @author Kenneth Lo
 * 
 */
public class SpeechRecognizerDemo {

	public static void main(String[] args) {

		// Step 1: Define the words that you need to recognize
		String[] recognizingWords = new String[5];
		recognizingWords[0] = "Stop Listening";
		recognizingWords[1] = "Internet";
		recognizingWords[2] = "Close Application";
		recognizingWords[3] = "Switch Application";
		recognizingWords[4] = "Desktop";

		// Step 2: Create the SpeechRecognizer Object
		SpeechRecognizer sr = new SpeechRecognizer(recognizingWords,
				"JSpeech.xml");

		try {

			// Step 3: Start the SpeechRecognizer
			sr.start();

			// Step 4: Recongize the word and take action, run until the user
			// speck "Stop"
			while (sr.isRunning()) {
				String command = sr.recognizedCommand();

				if (command.equals(recognizingWords[0])) {
					sr.close();
				} else if (command.equals(recognizingWords[1])) {
					Action.Launch(Application.INTERNET_EXPLORER).run();
				} else if (command.equals(recognizingWords[2])) {
					Action.KeyHoldAndClick(KeyEvent.VK_ALT, KeyEvent.VK_F4)
							.run();
				} else if (command.equals(recognizingWords[3])) {
					Action.KeyHoldAndClick(KeyEvent.VK_ALT, KeyEvent.VK_TAB)
							.run();
				} else if (command.equals(recognizingWords[4])) {
					Action.KeyHoldAndClick(KeyEvent.VK_WINDOWS, KeyEvent.VK_M)
							.run();
				}
			}

		} catch (EngineNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}
}
